package com.tbit.uqbike.webmanager.sms;


import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.webmanager.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * @author Kang
 * @date 2018年9月3日 下午1:40:25
 * @description 寻程数据发送短信
 */
@Slf4j
public class ApiStoreMsgUtils {
    /**
     * 接口地址
     */
    private static final String URL = "http://v.apistore.cn/api/v14/xsend";//模板短信

    /**
     * 不使用模板短信地址
     */
    private static final String contentUrl = "http://v.apistore.cn/api/v14/send";//短信

    /**
     * 短信模板发送
     *
     * @param phone
     * @param code
     * @param
     */
    public static void send(String phone, String code, String appKey, Integer modelId) {
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"code\":\"" + code + "\"}");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));
        log.info(json);
        if (json != null) {
            ApiStoreMsgResult result = JsonUtils.json2Obj(json, ApiStoreMsgResult.class);
            if (result.getError_code() != 0) {
                log.info("[用户验证码]寻程短信发送失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
            }
        }

    }

    /**
     * 骑行订单退款短信模板发送
     *
     * @param
     */
    public static void refundSend(String phone, String isMessage, String appKey, Integer modelId) {
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"isMessage\":\"" + isMessage + "\"}");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));
        log.info(json);
        if (json != null) {
            ApiStoreMsgResult result = JsonUtils.json2Obj(json, ApiStoreMsgResult.class);
            if (result.getError_code() != 0) {
                log.info("[用户验证码]寻程短信发送失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
            }
        }

    }

    /**
     * 根据模板发送短信
     *
     * @param phone   手机号码
     * @param appKey  key
     * @param modelId 模板id
     * @param jsonMsg 附件参数json字符串
     */
    public static void sendModelSms(String phone, String appKey, Integer modelId, String jsonMsg) {
        sendModelSms(phone, appKey, modelId, null, jsonMsg);
    }

    /**
     * 根据模板发送短信
     *
     * @param phone   手机号码
     * @param appKey  key
     * @param modelId 模板id
     * @param tplSign 短信签名
     * @param jsonMsg 附件参数json字符串
     */
    public static void sendModelSms(String phone, String appKey, Integer modelId, String tplSign, String jsonMsg) {
        Map<String, Object> params = new HashMap<>(16);
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        if (StringUtils.isNotBlank(tplSign)) {
            params.put("tpl_sign", tplSign);
        }
        params.put("mobile", phone);
        params.put("tpl_val", "{\"isMessage\":\"" + jsonMsg + "\"}");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));
        log.info(json);
        if (json != null) {
            ApiStoreMsgResult result = JsonUtils.json2Obj(json, ApiStoreMsgResult.class);
            if (result.getError_code() != 0) {
                log.info("寻程短信发送失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
            }
        }
    }


    /**
     * 通过文本发送短信
     *
     * @param phone
     * @param appKey
     * @param content
     */
    public static void sendContent(String phone, String appKey, String content) {
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("mobile", phone);
        params.put("content", content);
        String json = APIStore.doPost(contentUrl, APIStore.urlencode(params));
        log.info("寻程短信发送结果" + json);
        if (json != null) {
            ApiStoreMsgResult result = JSON.parseObject(json, ApiStoreMsgResult.class);
            if (result.getError_code() != 0) {
                System.out.println(result.getError_code());
                log.info("寻程短信发送失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
            }
        }


    }

}
